package com.learning.springcloudconsumer.controller;

import com.learning.springcloudconsumer.bean.User;
import com.learning.springcloudconsumer.service.UserService;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * (description)
 *
 * @author jinlong.liao on 2018/3/31
 */
@RestController
@RequestMapping("/batch")
public class BatchRequestController {

    @Autowired
    UserService userService;

    @GetMapping("/test1")
    public void test1() throws ExecutionException, InterruptedException {
        HystrixRequestContext hystrixRequestContext = HystrixRequestContext.initializeContext();

        Future<User> future1 = userService.futureUser(1);
        Future<User> future2 = userService.futureUser(2);
        Future<User> future3 = userService.futureUser(3);

        User user1 = future1.get();
        User user2 = future2.get();
        User user3 = future3.get();

        Thread.sleep(3000);

        Future<User> future4 = userService.futureUser(4);
        User user4 = future4.get();

        System.out.println("user1>>>" + user1);
        System.out.println("user2>>>" + user2);
        System.out.println("user3>>>" + user3);
        System.out.println("user4>>>" + user4);

        hystrixRequestContext.close();
    }
}
